//
//  VideoReleseController.m
//  OnionHome
//
//  Created by fengjie on 2019/10/15.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "VideoReleseController.h"
#import "CLPlayerView.h"
#import "ChooseMusicController.h"
#import "VideoCoverViewController.h"

@interface VideoReleseController ()
/**发布按钮*/
@property (nonatomic,strong)UIButton*releseBtn;

/**CLplayer*/
@property (nonatomic, weak) CLPlayerView *playerView;
/**<#属性#>*/
@property (nonatomic,strong)UITextField*textField;
@end

@implementation VideoReleseController

-(void)dealloc{
    //销毁播放器
    [self.playerView destroyPlayer];
    self.playerView = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KBlackColor;
    [self createUI];
    [self setNavBarItem];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndClearColor];
    if (self.playerView) {
        [self.playerView playVideo];
    }
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
   [center addObserver:self selector:@selector(keyboardDidShow)name:UIKeyboardDidShowNotification object:nil];
   [center addObserver:self selector:@selector(keyboardDidHide)name:UIKeyboardWillHideNotification object:nil];
    
}
    
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.playerView pausePlay];
       
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];

      [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}
-(void)keyboardDidShow{
    self.playerView.maskView.hidden = YES;
}

-(void)keyboardDidHide{
//    self.playerView.maskView.hidden = NO;
}
-(void)setModel:(VideoPlayModel *)model{
    _model = model;
    [self createPlayerView:model.url];
}
-(void)setUrl:(NSURL *)url{
    _url = url;
    [self createPlayerView:url];
}
-(void)createPlayerView:(NSURL *)url{

    //销毁播放器
    [_playerView destroyPlayer];
    CLPlayerView *playerView = [[CLPlayerView alloc] initWithFrame:CGRectZero];
    playerView.maskView.playBottomToolBar.hidden = YES;
    playerView.maskView.playButton.hidden = YES;
    _playerView = playerView;
//    _playerView.maskView.hidden = YES;
    [self.view addSubview:_playerView];
    [playerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(75);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-92);
    }];
    //    //重复播放，默认不播放
    _playerView.repeatPlay = YES;
    //视频地址
    _playerView.url = url;
    //播放
    [_playerView playVideo];
    //返回按钮点击事件回调
    [_playerView destroyPlay:^{
        DLog(@"播放器被销毁了");
    }];
    [_playerView backButton:^(UIButton *button) {
        DLog(@"返回按钮被点击");
    }];
    //播放完成回调
    [_playerView endPlay:^{
        DLog(@"播放完成");
    }];
}
-(void)createUI{
    UIImageView * bomView = [self createTextBgView];
    
    [self.view addSubview:self.releseBtn];
    [self.playerView addSubview:bomView];

    [self.releseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-20);
        make.width.mas_equalTo(124);
        make.height.mas_equalTo(34);
    }];
    
    [bomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-60);
    }];
}
-(void)setNavBarItem{

    [self addNavigationItemWithImageNames:@[@"back1"] isLeft:YES target:self action:@selector(backBtnClicked) tags:nil];
    
    CustomButton * oneItemView = [CustomButton buttonWithTitle:@"音乐" Image:@"音乐"];
    oneItemView.titleColor = UIColorFromRGB(0xF5F7F5);
    oneItemView.titleFont = SYSTEMFONT(10);
    kWeakSelf(weakSelf);
    oneItemView.handler = ^(CustomButton * _Nonnull view) {
        [weakSelf.navigationController pushViewController:[ChooseMusicController new] animated:YES];
    };
    CustomButton * twoItemView = [CustomButton buttonWithTitle:@"封面" Image:@"封面"];
    twoItemView.titleColor = UIColorFromRGB(0xF5F7F5);
    twoItemView.titleFont = SYSTEMFONT(10);
    twoItemView.handler = ^(CustomButton * _Nonnull view) {
        [weakSelf.playerView pausePlay];
        VideoCoverViewController * vc = [VideoCoverViewController new];
        if (weakSelf.model) {
            vc.url = [NSURL URLWithString:weakSelf.model.videoUrl];
        }else{
            vc.url = weakSelf.url;
        }
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    
    UIBarButtonItem * oneItem = [[UIBarButtonItem alloc] initWithCustomView:oneItemView];
    
    UIBarButtonItem * twoItem = [[UIBarButtonItem alloc] initWithCustomView:twoItemView];
     UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpaceBarButtonItem.width = 30;
    NSArray * array = @[twoItem,fixedSpaceBarButtonItem,oneItem];
    self.navigationItem.rightBarButtonItems = array;
    
}
-(void)backBtnClicked{
    [self.navigationController popViewControllerAnimated:YES];
}
-(UIButton*)releseBtn{
    if(!_releseBtn){
        _releseBtn=[UIButton new];
        [_releseBtn setTitle:@"发布" forState:UIControlStateNormal];
        [_releseBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x009966)] forState:UIControlStateNormal];
        _releseBtn.titleLabel.font = SYSTEMFONT(14);
    }
    return _releseBtn;
}
-(UITextField*)textField{
    if(!_textField){
        _textField=[UITextField new];
        _textField.placeholder = @"分享你此刻的心情~";
        _textField.placeholderColor = KWhiteColor;
        _textField.placeholderFont = SYSTEMFONT(14);
        _textField.textColor = KWhiteColor;
        _textField.font = SYSTEMFONT(14);

    }
    return _textField;
}
-(UIImageView *)createTextBgView{
    UIImageView * view = UIImageView.new;
    view.image = [UIImage createImageWithColor:RGBA(40, 40, 40, 0.5)];
    view.userInteractionEnabled = YES;
    ViewRadius(view, 4);
    
    UIButton * oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [oneBtn setBackgroundImage:IMAGE_NAMED(@"zhuanfa_2") forState:UIControlStateNormal];
    
    UIButton * twoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [twoBtn setBackgroundImage:IMAGE_NAMED(@"zhuanfa_3") forState:UIControlStateNormal];
    
    UIView * texBgView = UIView.new;
    texBgView.backgroundColor = RGBA(0, 0, 0, 0.5);
    ViewRadius(texBgView, 4);
    texBgView.userInteractionEnabled = YES;
    
    [view addSubview:texBgView];
    [view addSubview:oneBtn];
    [view addSubview:twoBtn];
    [texBgView addSubview:self.textField];

    [texBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(6);
        make.bottom.mas_equalTo(-6);
        make.height.mas_equalTo(40);
        make.right.mas_equalTo(oneBtn.mas_left).offset(-20);
    }];
    [twoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
        make.width.height.mas_equalTo(24);
    }];
    
    [oneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(twoBtn.mas_left).offset(-8);
        make.width.height.mas_equalTo(24);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-10);
    }];
    
    return view;
}
@end
